package com.chinaunicom.member.infrastructure.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.chinaunicom.common.constants.Constants;
import com.chinaunicom.member.domain.model.entity.UserEntity;
import com.chinaunicom.member.domain.repository.IMemberRepository;
import com.chinaunicom.member.infrastructure.database.mapper.UserMapper;
import com.chinaunicom.member.infrastructure.database.po.UserPO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author guomenghuai
 * @date 2024/1/8
 * @Description
 */
@Repository
public class MemberRepository implements IMemberRepository {

    @Resource
    private UserMapper userMapper;

    @Override
    public UserEntity queryUserByUserName(String username) {
        LambdaQueryWrapper<UserPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPO::getUsername, username);
        queryWrapper.eq(UserPO::getStatus, Constants.ENABLED);
        List<UserPO> userPOList = userMapper.selectList(queryWrapper);

        if (CollectionUtils.isEmpty(userPOList)) {
            return new UserEntity();
        }

        UserEntity userEntity = new UserEntity();
        UserPO userPO = userPOList.get(0);
        BeanUtils.copyProperties(userPO, userEntity);
        userEntity.setUserId(userPO.getId());
        return userEntity;
    }
}
